layui.config({
    base : "js/information/"
}).use(['form','layer','jquery','laypage','laydate'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        laydate = layui.laydate,
        $ = layui.jquery;

    var pageSize = 3;
    var currPage = 0;

    initList();

    function initList(){
        var title = $("#search_input").val().replace(/(^\s*)|(\s*$)/g,"");
        var navType = $("#navType").val();
        var floorType = $("#floorType").val();
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        // 页面初始化
        var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime,navType:navType,floorType:floorType};
        $.ajax({
            type: 'POST',
            url: "/information/getPublishedList",
            data: data,
            dataType: "text",
            success: function(data){
                var data = data.replace(/id\":(\d+),/g,"id\":\"$1\",");
                data = $.parseJSON(data);
                var html = template('informationList', data);
                document.getElementById('informationListTb').innerHTML = html;
                if(currPage >= 0) initPage(data["total"]);
            },
            error: function(){
                console.log("请求失败");
            }
        });
    }

    //查询
    $("#searchBtn").click(function(){
        var title = $("#search_input").val().replace(/(^\s*)|(\s*$)/g,"");
        var navType = $("#navType").val();
        var floorType = $("#floorType").val();
        var startTime = $("#startTime").val().replace(/(^\s*)|(\s*$)/g,"");
        var endTime = $("#endTime").val().replace(/(^\s*)|(\s*$)/g,"");

        if(startTime) startTime = startTime + " 00:00:00";
        if(endTime) endTime = endTime + " 23:59:59";

        //if(title != '' && typeof title != undefined){
            var index = layer.msg('查询中,请稍候',{icon: 16,time:false,shade:0.8});

            currPage = 0;
            var data = {page:currPage,pageSize:pageSize,title:title,startTime:startTime,endTime:endTime,navType:navType,floorType:floorType};
            $.ajax({
                type: 'POST',
                url: "/information/getPublishedList",
                data: data,
                dataType: "text",
                success: function(data){
                    var data = data.replace(/id\":(\d+),/g,"id\":\"$1\",");
                    data = $.parseJSON(data);
                    var html = template('informationList', data);
                    document.getElementById('informationListTb').innerHTML = html;
                    if(currPage >= 0) initPage(data["total"]);
                    layer.close(index);
                },
                error: function(){
                    layui.layer.tips('查询失败,请重试', '.layui-layer-setwin .layui-layer-close', {tips: 3});
                    layer.close(index);
                }
            });

            // setTimeout(function(){
            //     layer.close(index);
            // },2000);
        //}else{
            //layer.msg("请输入标题");
        //}
    });

    // 以下为对资讯的操作
    // 操作状态更改
    $(window).one("resize",function(){
        $("#informationListTb").on("click",".operate",function () {
            var infoId = $(this).attr("infoId");
            var $this = $(this);
            var $statusWrap = $this.closest("p").siblings("p.status_wrap");
            var optType = $(this).attr("optType");
            var html = "";
            var statusType = ""; // 操作类型
            var statusValue = ""; // 操作状态值

            if(optType == "toTop"){
                statusType = "toTop";
                statusValue = "11";
                html = '<span class="layui-show-md-inline-block animated bounceUp status label_1 toTop hover_close" optType="'+statusType+'" for="toTopBtn" infoId="'+infoId+'">置顶<i class="close">×</i></span>';
            }else if(optType == "slider"){
                statusType = "slider";
                statusValue = "60";
                html = '<span class="layui-show-md-inline-block animated bounceUp status label_1 slider hover_close" optType="'+statusType+'" for="sliderBtn" infoId="'+infoId+'">轮播<i class="close">×</i></span>';
            }else if(optType == "under"){
                statusType = "under";
                statusValue = "1";
                html = '<span class="layui-show-md-inline-block animated bounceUp status label_1 under hover_close" optType="'+statusType+'" for="underBtn" infoId="'+infoId+'">下架<i class="close">×</i></span>';
            }else if(optType == "intro"){
                statusType = "intro";
                statusValue = "0";
                html = '<span class="layui-show-md-inline-block animated bounceUp status label_1 intro hover_close" optType="'+statusType+'" for="introBtn" infoId="'+infoId+'">推荐<i class="close">×</i></span>';
            }else{
                statusType = "revoke";
                statusValue = "1";
            }

            var obj = {id:infoId,statusType:statusType,statusValue:statusValue};

            $.ajax({
                type: 'POST',
                url: "/information/updateInformationOptStatus",
                data: obj,
                success : function(data){
                    if(data["result"] == "success"){
                        layer.msg('状态更新成功');

                        if(statusType == "revoke"){
                            initList();
                            return;
                        }

                        $this.attr("disabled", "disabled");
                        $this.addClass("layui-btn-disabled");
                        $statusWrap.append(html);
                    }else{
                        layer.msg('状态更新失败');
                    }
                },
                error : function(){
                    layer.msg('状态更新失败');
                }
            })
        })
    }).resize();


    // 操作状态撤销
    $(window).one("resize",function(){
        $("#informationListTb").on("click",".close",function () {
            var $this = $(this).closest("span");
            var infoId = $this.attr("infoId");
            var forBtn = $this.attr("for");
            var optType = $this.attr("optType");
            $this.remove();
            var statusType = ""; // 操作类型
            var statusValue = ""; // 操作状态值


            if(optType == "toTop") {
                statusType = "toTop";
                statusValue = "";
            }else if(optType == "slider"){
                statusType = "slider";
                statusValue = "";
            }else if(optType == "under"){
                statusType = "under";
                statusValue = "0";
            }else if(optType == "intro"){
                statusType = "intro";
                statusValue = "1";
            }


            var obj = {id:infoId,statusType:statusType,statusValue:statusValue};
            $.ajax({
                type: 'POST',
                url: "/information/updateInformationOptStatus",
                data: obj,
                success : function(data){
                    if(data["result"] == "success"){
                        layer.msg('状态更新成功');
                        $("."+forBtn).removeAttr("disabled");
                        $("."+forBtn).removeClass("layui-btn-disabled");
                    }else{
                        layer.msg('状态更新失败');
                    }
                },
                error : function(){
                    layer.msg('状态更新失败');
                }
            })
        })
    }).resize();


    // 分页
    function initPage(total){
        laypage({
            cont : "pagination",
            pages : Math.ceil(total/pageSize),
            curr : currPage + 1,
            skip : true,
            jump : function(obj,first){
                currPage = obj.curr - 1;
                if(!first){
                    //window.location.href="/getAllInformationList?page="+obj.curr;
                    initList();
                    $('#informations_list thead input[type="checkbox"]').prop("checked",false);
                    form.render();
                }else{
                    $('#informations_list thead input[type="checkbox"]').prop("checked",false);
                    form.render();
                }
            }
        })
    }
});
